package com.xnx3.banmahuishou.wangge.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xnx3.BaseVO;
import com.xnx3.banmahuishou.core.entity.Daishoudian;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.service.UserService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.util.Page;
import com.xnx3.j2ee.util.Sql;

/**
 * 代收点管理
 * @author 王晓龙
 */
@Controller(value="BanmahuishouWanggeDaishoudianController")
@RequestMapping("/wangge/daishoudian/")
public class DaishoudianController extends BaseController {
	@Resource
	private SqlService sqlService;	
	@Resource
	private UserService userService;
	
	/**
	 * 代收点列表
	 */
	@RequestMapping("list${url.suffix}")
	public String list(HttpServletRequest request,Model model){
		Sql sql = new Sql(request);
		sql.setSearchColumn(new String[]{"daishoudian_name","state="});
		sql.appendWhere("wangge_userid = "+ getUserId());
		int count = sqlService.count("laji_daishoudian", sql.getWhere());
		Page page = new Page(count, 15, request);
		sql.setSelectFromAndPage("SELECT * FROM laji_daishoudian", page);
		sql.setDefaultOrderBy("laji_daishoudian.state ASC");
		List<Daishoudian> list = sqlService.findBySql(sql, Daishoudian.class);
		
		ActionLogUtil.insert(request, "代收点管理", "第"+page.getCurrentPageNumber()+"页");
		
		model.addAttribute("page", page);
		model.addAttribute("list", list);
		return "/wangge/daishoudian/list";
	}

	/**
	 * 修改代收点
	 * @param id 要修改的id
	 */
	@RequestMapping("edit${url.suffix}")
	public String edit(
			@RequestParam(value = "id", required = false, defaultValue="0") int id,
			Model model, HttpServletRequest request){
		Daishoudian entity;
		
			//修改
			entity = sqlService.findById(Daishoudian.class, id);
			if(entity == null){
				return error(model, "要修改的代收点不存在");
			}
			ActionLogUtil.insert(request, "进入修改代收点页面", entity.toString());
		
		//编辑页面
		model.addAttribute("entity", entity);
		return "/wangge/daishoudian/edit";
	}
	
	/**
	 * 修改的代收点信息进行保存
	 */
	@RequestMapping(value="save.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO variableSave(Daishoudian inputEntity, HttpServletRequest request){
		Daishoudian entity = null;			
		
		//修改
		entity = sqlService.findById(Daishoudian.class, inputEntity.getId());
		if(entity == null){
			return error("要修改的目标不存在");
		}
		
		//保存
		entity.setDaishoudianName(inputEntity.getDaishoudianName());
		entity.setDaishoudianUsername(inputEntity.getDaishoudianUsername());
		entity.setDaishoudianPhone(inputEntity.getDaishoudianPhone());
		entity.setDaishoudianAddress(inputEntity.getDaishoudianAddress());
		entity.setLatitude(inputEntity.getLatitude());
		entity.setLongitude(inputEntity.getLongitude());
		entity.setRemark(inputEntity.getRemark());
		entity.setDaishoudianImage(inputEntity.getDaishoudianImage());
		entity.setQishouUsername(inputEntity.getQishouUsername());
		entity.setQishouPhone(inputEntity.getQishouPhone());
		sqlService.save(entity);
		
		ActionLogUtil.insertUpdateDatabase(request, entity.getId(), "保存代收点信息", entity.toString());
		return success();
	}
	
	/**
	 * 审核同意
	 * @param id id编号，对应 laji_daishoudian.id
	 */
	@RequestMapping(value="reviewEdit.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO reviewEdit(HttpServletRequest request,
			@RequestParam(value = "id", required = false, defaultValue="0") int id){	
		
		Daishoudian daishoudian = sqlService.findById(Daishoudian.class, id);
		if(daishoudian == null){
			return error("要同意的目标不存在");
		}
		daishoudian.setState(Daishoudian.STATE_APPROVED);

		sqlService.save(daishoudian);
		ActionLogUtil.insertUpdateDatabase(request, "审核同意","Id为" + id + "的代收点，审核通过");
		return success();
	}
	
	/**
	 * 审核拒绝
	 * @param id id编号，对应 laji_daishoudian.id
	 */
	@RequestMapping("refuseedit${url.suffix}")
	public String refuseedit(
			@RequestParam(value = "id", required = false, defaultValue="0") int id,
			Model model, HttpServletRequest request){
		Daishoudian entity;
		
		//查询要拒绝的信息
		entity = sqlService.findById(Daishoudian.class, id);
		if(entity == null){
			return error(model, "要拒绝的代收点不存在");
		}
		
		//编辑页面
		model.addAttribute("entity", entity);
		return "/wangge/daishoudian/refuseedit";
	}
	/**
	 * 拒绝原因的进行保存
	 */
	@RequestMapping(value="refuse.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO refuse(Daishoudian inputEntity, HttpServletRequest request){
		Daishoudian entity = null;			
	
		//查询要拒绝的信息
		entity = sqlService.findById(Daishoudian.class, inputEntity.getId());
		if(entity == null){
			return error("要拒绝的目标不存在");
		}
		//保存
		entity.setRefuseReason(inputEntity.getRefuseReason());
		entity.setState(Daishoudian.STATE_REJECTED);
		sqlService.save(entity);
		
		ActionLogUtil.insertUpdateDatabase(request, entity.getId(), "保存拒绝原因", entity.toString());
		return success();
	}
	
	/**
	 * 冻结代收点
	 * @param id id编号，对应 laji_daishoudian.id
	 */
	@RequestMapping(value="frozen.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO frozen(HttpServletRequest request,
			@RequestParam(value = "id", required = false, defaultValue="0") int id){	
		
		Daishoudian daishoudian = sqlService.findById(Daishoudian.class, id);
		if(daishoudian == null){
			return error("要冻结的目标不存在");
		}
		daishoudian.setState(Daishoudian.STATE_FROZEN);

		sqlService.save(daishoudian);
		ActionLogUtil.insertUpdateDatabase(request, "冻结","Id为" + id + "的代收点，已冻结");
		return success();
	}
	
	/**
	 * 解冻代收点
	 * @param id id编号，对应 laji_daishoudian.id
	 */
	@RequestMapping(value="thaw.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO thaw(HttpServletRequest request,
			@RequestParam(value = "id", required = false, defaultValue="0") int id){	
		
		Daishoudian daishoudian = sqlService.findById(Daishoudian.class, id);
		if(daishoudian == null){
			return error("要解冻的目标不存在");
		}
		daishoudian.setState(Daishoudian.STATE_APPROVED);

		sqlService.save(daishoudian);
		ActionLogUtil.insertUpdateDatabase(request, "解冻上线","Id为" + id + "的代收点，已解冻上线");
		return success();
	}

	/**
	 * 代收点详情
	 * @param id 要查看详情的代收点的id, 对应 laji_daishoudian.id
	 */
	@RequestMapping("view${url.suffix}")
	public String view(HttpServletRequest request,
			@RequestParam(value = "id", required = true) int id,Model model){
		Daishoudian daishoudian = sqlService.findById(Daishoudian.class, id);
		if(daishoudian == null){
			return error(model, "要查看的代收点不存在");
		}
		
		ActionLogUtil.insert(request, daishoudian.getId(), "代收点详情", daishoudian.toString());
		
		model.addAttribute("daishoudian", daishoudian);
		return "/wangge/daishoudian/view";
	}
}
